package cn.liontalk.demo;

/**
 * @author: 周哲
 * @package: cn.liontalk.demo
 * @description:
 * @date: 2019/1/26 16:18
 * @version: V1.0
 */
public class SynchronizedException9 implements Runnable {

    static SynchronizedException9 instance = new SynchronizedException9();

    public void run() {
        if (Thread.currentThread().getName().equals("Thread-0")) {
            test1();
        } else {
            test2();
        }
    }

    public synchronized void test1() {
        System.out.println("我是对象锁的lock2 :  代码块形式：" + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // System.out.println(Thread.currentThread().getName() + "：运行完成");
        throw new RuntimeException();
    }


    public synchronized void test2() {
        System.out.println("我是对象锁的lock2 :  代码块形式：" + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "：运行完成");
    }

    public static void main(String[] args) {

        Thread thread = new Thread(instance);
        Thread thread1 = new Thread(instance);

        thread.start();
        thread1.start();

        while (thread.isAlive() || thread1.isAlive()) {

        }
        System.out.println("FINISHED");
    }
}
